<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
    String contextPath = (String) request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" href="<%=contextPath %>/css/reset.css"
	rel="stylesheet" />
<link rel="stylesheet" href="<%=contextPath %>/css/edit.css" type="text/css" />
<link rel="stylesheet" type="text/css"
	href="<%=contextPath %>/js/jquery-easyui-1.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="<%=contextPath %>/js/jquery-easyui-1.3/themes/icon.css">
<link href="<%=contextPath %>/js/uploadify/uploadify.css" rel="stylesheet" type="text/css" />	
<script type="text/javascript"
	src="<%=contextPath %>/js/jquery-easyui-1.3/jquery-1.7.2.min.js"></script>
<script type="text/javascript"
	src="<%=contextPath %>/js/jquery-easyui-1.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="<%=contextPath %>/js/jquery-easyui-1.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="<%=contextPath %>/js/uploadify/jquery.uploadify.min.js"></script>
<script>
	var selectedIds = "";
	$(function() {
		$('#wndAdd').window({closed:true});//  data-options="closed:true"
		$('#wndSearch').window({closed:true});
		$('#wndBatchImp').window({closed:true});
		$('#wndDetail').window({closed:true});
		$('#dataTable').datagrid({
			title : '科室管理',
			height : 500,
			nowrap : true,
			autoRowHeight : false,
			striped : true,
			collapsible : false,
			url : '<%=contextPath%>/admin/deptAction.do?method=deptList',
			sortName : 'id',
			sortOrder : 'desc',
			remoteSort : false,
			pageList: [10,30,50],
			idField : 'id',
			onCheck:function(rowIndex,rowData){
				selectedIds+=rowData.id+",";
			},
			onSelectAll:function(rows){
				for(var i=0;i<rows.length;i++){
					selectedIds+=rows[i].id+",";
				}
			},
			onUncheckAll:function(rows){
				selectedIds='';
			},
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, 
			
			{
				title : 'ID',
				field : 'id',
				width : 30,
				sortable : true
			},
			{
				field : 'level',
				title : '科室级别',
				width : 100,
				sortable : true,
				formatter:function(value,row,index){
					if(row.level=='1') return '一级';
					if(row.level=='2') return '二级';
					return '';
				}
			},
			{
				field : 'name',
				title : '科室名称',
				width : 100,
				sortable : true
			}, 
			{
				field : 'self_code',
				title : '内部编码',
				width : 50
			}, 
			{
				field : 'hospital_id',
				title : '所属医院',
				width : 200,
				sortable : false,
				formatter:function(value,row,index){
					return row.hos_name;
				}
			}, 
			{
				field : 'remark',
				title : '介绍',
				width : 200
			}, 
			
			{
				field : 'opt',
				title : '操作',
				width : 100,
				align : 'center',
				rowspan : 2,
				formatter : function(value, row,index) {
					return '<a style="color:red" href="#" mce_href="#" onclick="viewSingle('+row.id+')">详情</a>&nbsp;&nbsp;<a style="color:red" href="#" mce_href="#" onClick="editSingle('+row.id+')">编辑</a>&nbsp;&nbsp;<a style="color:red" href="#" mce_href="#" onClick="deleteSingle('+row.id+')">删除</a>';
				}
			} ] ],

			pagination : true,
			rownumbers : true,
			
			toolbar : [ {
				id : 'btnadd',
				text : '新增',
				iconCls : 'icon-add',
				handler : function() {
					$('#btnsave').linkbutton('enable');
					$("#addForm").form('clear');
					$('#wndAdd').window({
						title:"新增科室",
						closed:false,
						width : 700,
						height : 500,
						top:($(window).height()-500)*0.5,   
			            left:($(window).width()-700)*0.5,
						modal : true
					});
				}
			},
			{
				id : 'btnbatch',
				text : '批量导入',
				iconCls : 'icon-add',
				onClose:function(){
					$('#dataTable').datagrid('reload');
				},
				handler : function() {
					//$('#btnbatch').linkbutton('enable');
					//清空导入页面的提示信息
					$("#batch_step1").html('');
		            $("#batch_step2").html('');
		            $("#batch_step3").html('');
					$("#wndBatchImp").window({
						title:"批量导入科室信息",
						closed:false,
						width:500,
						height:300,
						top:($(window).height()-300)*0.5,   
			            left:($(window).width()-500)*0.5,
						modal:true,
						onClose:function(){
							$('#dataTable').datagrid('reload');
						}
					});
				}
			},
			{
				id : 'btncut',
				text : '删除	',
				iconCls : 'icon-cut',
				handler : function() {
					//$('#btnsave').linkbutton('enable');
					if(selectedIds.indexOf(',')!=-1){
					deleteSingle(selectedIds);
					}else{
						$.messager.alert('提示','没有选择任何数据!');
					}
				}
			},{
				id : 'btnsearch',
				text : '条件查询',
				iconCls : 'icon-search',
				handler : function() {
					$("#searchForm").form('clear');
					$('#wndSearch').window({
						title:"查询",
						closed:false,
						width : 950,
						height : 200,
						top:($(window).height()-200)*0.5,   
			            left:($(window).width()-950)*0.5,
						modal : true
					});
				}
			}]
		});
		
		
		var p = $('#dataTable').datagrid('getPager');
		$(p).pagination({
			onBeforeRefresh : function() {
				//alert('before refresh');
			}
		});
		
		//查询
		$("#btSch").click(function(){
			var v_hospital_id = $("#sch_hospital_id").combobox('getValue');
			var v_level = $("#sch_level").val();		
			var v_name = $("#sch_name").val();
			var v_self_code = $("#sch_self_code").val();
			$('#dataTable').datagrid('reload',{
				hospital_id:v_hospital_id,
				level:v_level,
				name:v_name,
				self_code:v_self_code
			});
		});
		
		//清除
		$("#btClear").click(function(){
			$("#searchForm").form('clear');
		});
		
		//保存
		$("#bt_submit").click(function(){
			
			$("#addForm").form('submit',{
				url:'<%=contextPath%>/admin/deptAction.do?method=save',
				onSubmit:function(){
					
					return validateForm();
				},		
				success:function(data){
					var data = eval('(' + data + ')'); 
					$.messager.alert('提示',data.msg);
					if(data.flag=='success'){
					$("#addForm").form('clear');
					$('#wndAdd').window('close');
					$('#dataTable').datagrid('reload');
					}
				}
			});
		});
		
			
			
		//关闭新增窗口
		$("#bt_close").click(function(){
			$('#wndAdd').window('close');
		});
		
		//父级科室联动
		$("#level").change(function(){
			if($(this).val()=='2'){
				var hospital_id = $("#hospital_id").combobox('getValue');
				if(hospital_id!=''){
					$("#parent_id").combobox({
						url:'<%=contextPath %>/admin/deptAction.do?method=initParentSec&hospital_id='+hospital_id,
						valueField:'id',
						textField:'name',
						onLoadSuccess:function(){
							var hid_parent_id = $("#hid_parent_id").val();
							if(hid_parent_id!=''&&hid_parent_id!='0'){
								$("#parent_id").combobox('setValue',hid_parent_id);
							}
						}
					});
				}
				//$("#parent_id").combobox('reload','<%=contextPath %>/admin/deptAction.do?method=initParentSec&hospital_id='+hospital_id);
				
			}else{
				$("#parent_id").combobox('loadData',[{}]);
				$("#parent_id").combobox('setValue','');
			}
		});
		//父级科室联动
		$("#catParent").change(function(){
			if($(this).val()=='2'){
				var hospital_id = $("#hospital_id").combobox('getValue');
				if(hospital_id!=''){
					$("#parent_id").combobox({
						url:'<%=contextPath %>/admin/deptAction.do?method=initParentSec&hospital_id='+hospital_id,
						valueField:'id',
						textField:'name',
						onLoadSuccess:function(){
							var hid_parent_id = $("#hid_parent_id").val();
							if(hid_parent_id!=''&&hid_parent_id!='0'){
								$("#parent_id").combobox('setValue',hid_parent_id);
							}
						}
					});
				}
				//$("#parent_id").combobox('reload','<%=contextPath %>/admin/deptAction.do?method=initParentSec&hospital_id='+hospital_id);
				
			}else{
				$("#parent_id").combobox('loadData',[{}]);
				$("#parent_id").combobox('setValue','');
			}
		});
		
		//上传具体图片文件
		$("#file_image_id").uploadify({
			formData	  :{'system':'reserve-system','busiType':'add','busiTable':'t_section','busiId':'image_id','module':'科室管理'},
			height        : 30,
	        swf           : '<%=contextPath%>/js/uploadify/uploadify.swf',
	        uploader      : '<%=contextPath%>/commons/swfupload/upload.do?method=upload&jsessionid=<%=session.getId()%>',
	        width         : 120,
	        fileTypeDesc : 'Image Files',
	        fileTypeExts : '*.gif; *.jpg; *.png',
			buttonText:'选择图片上传',
			onUploadSuccess : function(file, data, response) {
				var data = eval('('+data+')');
				$("#img_id").attr('src','<%=contextPath%>/upload/'+file.name);
				$("#hid_image_id").val(data.uploadId);
	        }


		});
		//批量导入
		$("#file_imp").uploadify({
			formData	  :{'system':'reserve-system','busiType':'add','busiTable':'t_section','busiId':'image_id','module':'科室管理'},
			height        : 30,
	        swf           : '<%=contextPath%>/js/uploadify/uploadify.swf',
	        uploader      : '<%=contextPath%>/commons/swfupload/upload.do?method=upload&jsessionid=<%=session.getId()%>',
	        width         : 120,
	        fileTypeDesc : 'xml 文件',
	        fileTypeExts : '*.xml; *.xlsx',
			buttonText:'选择文件上传',
			onUploadSuccess : function(file, data, response) {
				var data = eval('('+data+')');
				$("#img_id").attr('src','<%=contextPath%>/upload/'+file.name+'.jpg');
				//$("#hid_image_id").val(data.uploadId);
	            //alert('The file ' + file.name+ ' was successfully uploaded with a response of ' + response + ':' + data);
	            $("#batch_step1").html('文件上传成功');
	            $("#batch_step2").html('开始校验并保存信息');
	            $.ajax({
	            	url:'<%=contextPath%>/admin/deptAction.do?method=importData&upload_id='+data.uploadId,
	            	type:'get',
	            	success:function(data){
	            		var data = eval('('+data+')');
	    	            $.messager.alert('提示',data[0].msg);
	    	            $("#batch_step3").html(data[0].msg+"<br/>");
	    	            if(data[0].flag=='fail'){
	    	            	$("#batch_step3").append("详细:<br/>");
	    	            	for(var i=1;i<data.length;i++){
	    	            	$("#batch_step3").append(data[i]+"<br/>");
	    	            	}
	    	            }
	            		
	            	}
	            });
	        }


		});
	});
	
	//查看详细
	function viewSingle(id){
		document.getElementById("frmDetail").src="<%=contextPath%>/admin/deptAction.do?method=view&id="+id;
		$('#wndDetail').window({
			title:"详情",
			closed:false,
			width : 600,
			height : 400,
			top:($(window).height()-400)*0.5,   
            left:($(window).width()-600)*0.5,
			onload:function(){
			
			},
			modal : true
		});
	}
	
	//编辑
	function editSingle(id){
		$("#addForm").form('clear');
		$.ajax({
			url:"<%=contextPath%>/admin/deptAction.do?method=edit&id="+id+"&radom="+Math.random(),
			type:"post",
			success:function(data){
				var data = eval('('+data+')');
				$("#hospital_id").combobox('setValue',data.hospitalId);
				$("#id").val(data.id);
				$("#name").val(data.name);
				$("#hid_parent_id").val(data.parentId);
				$("#level").val(data.level);
				$("#level").trigger('change');
				//if(data.level=='2'){
				//$("#parent_id").combobox('setValue',data.parentId);
				//}
				$("#self_code").val(data.selfCode);
				$("#hid_image_id").val(data.image_id);
				if(data.path==''||data.path=='null'){
				$("#img_id").attr('src','<%=contextPath%>/img/no_photo.jpg');
					
				}else{
					
				$("#img_id").attr("src",'<%=contextPath%>/upload'+data.path);
				}
				$("#remark").val(data.remark);
			}
		});
		$('#wndAdd').window({
			title:"编辑",
			closed:false,
			width : 700,
			height : 500,
			top:($(window).height()-500)*0.5,   
            left:($(window).width()-700)*0.5,
			//href:"<%=contextPath%>/admin/deptAction.do?method=edit&id="+id,
			onload:function(){
			//var data = eval('('+data+')');
			
			},
			modal : true
		});
	}
	
	//删除
	function deleteSingle(id){
		$.messager.confirm('删除','确定删除选择的科室吗?',function(r){
			if(r){
				$.ajax({
					url:"<%=contextPath%>/admin/deptAction.do?method=delete&id="+id,
					type:"post",
					success:function(data){
					var data = eval('(' + data + ')');
					$.messager.alert('提示',data.msg);
					selectedIds = "";//多选时保存id  删除成功清空
					$('#dataTable').datagrid('reload');
					}
				});
			}
		});
	}
	
	
	//自己验证  
	function validateForm(){
		var msg = "";
		//医院
		if($("#hospital_id").combobox('getValue')=='') msg+="请选择所属医院!<br/>";
		//名称
		if($.trim($("#name").val())=='') msg+="请输入科室名称!<br/>";
		//级别
		if($("#level").val()==null||$("#level").val()=='') msg+="请选择科室级别!<br/>";
		if($("#level").val()=='2'&&$("#parent_id").combobox('getValue')=='') msg+="科室级别为二级时必须选择所属父科室!<br/>";
		if($.trim($("#self_code").val())=='') msg+="请输入内部编码!<br/>";
		if(msg!=''){
			$.messager.alert('错误',msg);
			return false;
		}else{
			return true;
		}
	}
	
</script>
<title>科室管理</title>
</head>
<body>
<div id="wndSearch" class="easyui-window">
<div class="easyui-panel admin_search" data-option="fit:true" style="margin:2px 2px">
	<form id="searchForm">
		<table>
			<tr>
				<td class="textr w80" style="width: 60px;">所属医院:</td>
				<td class="w180">
				<select id="sch_hospital_id" class="easyui-combobox" name="hospital_id" data-options="editable:false,valueField:'id',textField:'name',url:'<%=contextPath %>/admin/deptAction.do?method=initHospital'" >
					<option value="" selected>所有医院</option>
				</select>
				</td>
				<td class="textr w80">科室级别:</td>
				<td class="w180">
					<select class="admin-select" name="sch_cat_parent" id="sch_cat_parent">
						<option value="">所有</option>
					</select>
					
				</td>
				<td class="textr w80">科室名:</td>
				<td><input type="text" class="admin-input" name="name" id="sch_name"></td>
				<td class="textr">内部编码:</td>
				<td><input type="text" class="admin-input" name="self_code" id="sch_self_code"></td>
			</tr>
		</table>
		</form>
		<div style="padding-left: 320px;">
			<a href="javascript:void(0);" id="btSch" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">查 询</a>&nbsp;
			<a href="javascript:void(0);" id="btClear" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">清除</a>
		</div>
	</div>
	</div>
	<div class="easyui-panel" data-option="fit:true" style="overflow:auto;">
		<table id="dataTable" class="easyui-datagrid">
		</table>
	</div>
	<div id="wndAdd"  class="easyui-window">
		<div class="admin_search"
			style="width: 580px;">
			
			<form id="addForm" method="post">
			<input type="hidden" name="method" id="method" value="save"/>
			<input type="hidden" name="id" id="id"/>
			<table>
				<tr>
					<td class="textr w80">所属医院：</td>
					<td>
					<input id="hospital_id" class="easyui-combobox" name="hospitalId" data-options="editable:false,valueField:'id',textField:'name',url:'<%=contextPath %>/admin/deptAction.do?method=initHospital',onSelect:function(rec){var url='<%=contextPath %>/admin/deptAction.do?method=initParentSec&hospital_id='+rec.id;$('#parent_id').combobox('reload',url);}"/>
					</td>
					<td class="textr w80">科室名称：</td>
					<td class="w180"><input type="text" id="name" name="name" class="admin-input"></td>
				</tr>
				<tr>
					<td class="textr w80">科室级别：</td>
					<td>		
						<input class="easyui-combobox"  id="catParents" name="catParents" />
					</td>
					<td class="textr w80">所属科室：</td>
					<td class="w180">
					<input id="parent_id" class="easyui-combobox" name="parentId" data-options="editable:false,valueField:'id',textField:'name'"/>
					<input id="hid_parent_id" name="hid_parent_id" type="hidden"/>
					</td>
				</tr>
				<tr>
					<td class="textr w80">内部编码</td>
					<td colspan="3"><input type="text" id="self_code" name="selfCode" class="admin-input"></td>	
				</tr>	
				<tr>
				<td class="textr w80">科室图片：</td>
					<td class="w180"><input type="file" id="file_image_id" name="f_image_id" class="admin-input">
					<input type="hidden" id="hid_image_id" name="imageId"/></td>
					<td colspan="2"><img id="img_id" src="" width="80" height="50"/></td>		
				</tr>							
				
				<tr>
					<td class="textr w80">描述：</td>
					<td colspan="3">
					<textarea rows="3" cols="75" style="border:solid 1px #D3D3D3" id="remark" name="remark"></textarea>
					</td>
				</tr>
			</table>
			</form>
			<div style="text-align: center;">
			<a href="javascript:void(0);" id="bt_submit" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">确认</a>&nbsp;&nbsp;
			<a href="javascript:void(0);" id="bt_close" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">关闭</a>
			</div>
		</div>
	</div>
	
	<div id="wndBatchImp"  class="easyui-window">
		<input type="file" id="file_imp" name="file_imp"/>
		<p id="batch_step1"></p>
		<p id="batch_step2"></p>
		<p id="batch_step3"></p>
	</div>
	<div id="wndDetail"  class="easyui-window">
	<iframe id="frmDetail" src="" width="100%" height="370px;"
			frameborder="0" scrolling="no" style="border: 0px none;" ></iframe>
	</div>
</body>
</html>